#博客详情页
from time import sleep

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Untils import BlogDriver

class BlogDetail:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_detail.html?blogId=158477"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
    #登录状态下博客详情页的测试
    def DetailTestByLogin(self):
        #检查标题
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.title")
        #检查时间
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.date")
        #检查内容
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating")
        self.driver.back()

        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(2) > a").click()
        #检查编辑操作
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)").click()
        self.driver.find_element(By.CSS_SELECTOR,"#title")
        self.driver.back()
        #检查删除操作
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
        alert = WebDriverWait(self.driver, 20).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alert.accept()
        self.driver.get(self.url)
    #未登录状态下博客详情页的测试
    def DetailTestByNotLogin(self):
        # 点击注销
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=158477")

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查是否登录了
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=158477")